之前提到過 swift demangle
除了使用 swift-demangle
cli 以外還有其他方式可以使用
這邊參考 Swift Secrets
的實作方式
/// SWIFT_RUNTIME_EXPORT
/// https://github.com/apple/swift/blob/a4e71aa819cfaa25d512555c351377efa968aa68/include/swift/Demangling/Demangle.h#L668
@_silgen_name("swift_demangle")
private func _stdlib_demangleImpl(_ mangledName: UnsafePointer<Int8>?, mangledNameLength: UInt, outputBuffer: UnsafeMutablePointer<Int8>?, outputBufferSize: UnsafeMutablePointer<UInt>?, flags: UInt32) -> UnsafeMutablePointer<Int8>?
func demangle(_ symbol: String) -> String? {
return symbol.withCString { (cString: UnsafePointer<Int8>) -> String? in
let std: UnsafeMutablePointer<Int8>? = _stdlib_demangleImpl(
cString,
mangledNameLength: UInt(strlen(cString)),
outputBuffer: nil,
outputBufferSize: nil,
flags: 0
)
if let demangled: UnsafeMutablePointer<Int8> = std {
let out: String = String(cString: demangled)
free(demangled)
return out
}
return nil
}
}
demangle(usr)
.
SWIFT_RUNTIME_EXPORT
char *swift_demangle(const char *mangledName,
size_t mangledNameLength,
char *outputBuffer,
size_t *outputBufferSize,
uint32_t flags)